We’re going to make some plotly plots.

Load packages and data

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6      ✔ purrr   0.3.4 
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.4.1 
## ✔ readr   2.1.2      ✔ forcats 0.5.2 
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(p8105.datasets)
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
data("nyc_airbnb")

nyc_airbnb = 
  nyc_airbnb %>%
  mutate(rating = review_scores_location / 2) %>%
  select(
    borough = neighbourhood_group, neighbourhood, price, room_type, lat, long, rating) %>%
  filter(
    borough == "Manhattan",
    room_type == "Entire home/apt",
    price %in% 100:500
  ) %>%
  drop_na(rating)

Let’s make a scatterplot!!

nyc_airbnb %>%
  mutate(
    text_label = str_c("Price: ", price, "\nRating: ", rating) #combine string "Price" and price var
  ) %>%
  plot_ly(
    x = ~lat, y = ~long, color = ~price,
    type = "scatter", mode = "markers",
    alpha = 0.5, text = ~text_label
  )

Can we make boxplots??

nyc_airbnb %>%
  mutate(neighbourhood = fct_reorder(neighbourhood, price)) %>%
  plot_ly(
    y = ~price, color = ~neighbourhood,
    type = "box", colors = "viridis")

Can we make a bar plot?

nyc_airbnb %>%
  count(neighbourhood) %>%
  mutate(neighbourhood = fct_reorder(neighbourhood, n)) %>%
  plot_ly(
    x = ~neighbourhood, y = ~n,
    type = "bar")

ggplotly …

ggp_scatterplot = 
nyc_airbnb %>%
  ggplot(aes(x = lat, y = long, color = price)) +
  geom_point()

ggplotly(ggp_scatterplot)

easy to create, but slow interaction

Create a dashboard …

Not here though.